/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/5/22.
//

#include "sql/stmt/update_stmt.h"
#include "common/log/log.h"
#include "common/value.h"
#include "sql/parser/expression_binder.h"
#include "sql/stmt/filter_stmt.h"
#include "storage/db/db.h"
#include "storage/field/field_meta.h"
#include "storage/table/table.h"
#include <string>

UpdateStmt::~UpdateStmt()
{
  if (nullptr != filter_stmt_) {
    delete filter_stmt_;
    filter_stmt_ = nullptr;
  }
}

RC UpdateStmt::create(Db *db, UpdateSqlNode &update, Stmt *&stmt)
{
  
  //这里仅支持一个字段的更新 这里需要判断 table_name attr的name和value不为空、
  //用处是什么？
  const char *table_name = update.relation_name.c_str();
  const char *attribute_name = update.attribute_name.c_str();

  BinderContext binder_context;

  //此时为检查未检查属性名是否为空
  if (nullptr == db || nullptr == table_name || nullptr == attribute_name) {
    LOG_WARN("invalid argument. db=%p, table_name=%p, value_num=%p",
        db, table_name, attribute_name);
    return RC::INVALID_ARGUMENT;
  }

  // check whether the table and attr exists
  Table *table = db->find_table(table_name);
  if (nullptr == table) {
    LOG_WARN("no such table. db=%s, table_name=%s", db->name(), table_name);
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }
  //类型检查要record里面

  
  // collect query fields in `select` statement
  //vector<unique_ptr<Expression>> bound_expressions;
  ExpressionBinder expression_binder(binder_context);

  //因为这里condition改动了，因此要传其他的
  for (auto &node: update.conditions) {
    vector<unique_ptr<Expression>> left_condition_expressions;
    //左边解析一下
    RC rc = expression_binder.bind_expression(node.left, left_condition_expressions);
    if (OB_FAIL(rc)) {
      LOG_INFO("bind expression failed. rc=%s", strrc(rc));
      return rc;
    }
    node.left.reset(left_condition_expressions.front().release());
    

    vector<unique_ptr<Expression>> right_condition_expressions;

    rc = expression_binder.bind_expression(node.right, right_condition_expressions);
    if (OB_FAIL(rc)) {
      LOG_INFO("bind expression failed. rc=%s", strrc(rc));
      return rc;
    }
    node.right.reset(right_condition_expressions.front().release());
  }


  // create filter statement in `where` statement
  FilterStmt *filter_stmt = nullptr;
  RC rc = FilterStmt::create(db, table, nullptr,
      update.conditions.data(),
      static_cast<int>(update.conditions.size()),
      filter_stmt);
      
  if (rc != RC::SUCCESS) {
    LOG_WARN("cannot construct filter stmt");
    return rc;
  }

  // everything alright
  UpdateStmt *update_stmt = new UpdateStmt();
  //这里面涉及道常量的转化，注意const_cast只能转化指针类型的 而parse解析穿过来的是Value 而不是Value*
  update_stmt->table_ = table;
  update_stmt->value_ = &const_cast<UpdateSqlNode&>(update).value;
  update_stmt->field_name_= const_cast<char *>(attribute_name);
  update_stmt->filter_stmt_ = filter_stmt;
  update_stmt->value_amount_ = 1;

  stmt = update_stmt;
  return RC::SUCCESS;
}